package com.superman.view;

import com.superman.entity.Record_User_Car;
import com.superman.entity.User;
import com.superman.util.JPanelCarAdmin;
import com.superman.util.JPanelRecordAdmin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class AdminUI {
    private User user;
    Query query = new Query();
    JPanelCarAdmin jPanelCar = new JPanelCarAdmin();
    JPanelRecordAdmin jPanelRecord = new JPanelRecordAdmin();

    private JFrame frame = new JFrame("管理平台");
    private Container container = frame.getContentPane();

    //上
    private JPanel upside_panel = new JPanel();
    private JButton queryCar_btn = new JButton("查看所有汽车");
    private JButton sortByRentASC_btn = new JButton("按价格升序排序");
    private JButton sortByRentDESC_btn = new JButton("按价格降序排序");
    private JLabel queryByCategory_lab = new JLabel("按类型搜索");
    private JLabel queryByBrand_lab = new JLabel("按品牌搜索");
    private JComboBox queryByCategory_cb = new JComboBox(query.queryCategory());
    private JComboBox queryByBrand_cb = new JComboBox(query.queryBrand());
    private JButton queryByCategory_btn = new JButton("搜索");
    private JButton queryByBrand_btn = new JButton("搜索");

    //中：列表展示区域
    private JPanel middle_panel = new JPanel();
    private JPanel panel_1;//所有汽车
    private JPanel panel_2;//价格升序
    private JPanel panel_3;//价格降序
    private JPanel panel_4;//按类型搜索
    private JPanel panel_5;//按品牌搜索
    private JPanel panel_6;//全部租车记录

    //下
    private JPanel downside_panel = new JPanel();
    private JButton rent_btn = new JButton("添加汽车");
    private JButton return_btn = new JButton("修改汽车信息");
    private JButton record_btn = new JButton("查看租车记录");
    private JButton backup_btn = new JButton("返回登录");

    public AdminUI(User user){
        frame.setSize(1000,500);
        container.setLayout(new BorderLayout());
        initFrame(user);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void initFrame(User user){
        //上
        upside_panel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
        upside_panel.add(queryCar_btn);
        upside_panel.add(sortByRentASC_btn);
        upside_panel.add(sortByRentDESC_btn);
        upside_panel.add(queryByCategory_lab);
        upside_panel.add(queryByCategory_cb);
        upside_panel.add(queryByCategory_btn);
        upside_panel.add(queryByBrand_lab);
        upside_panel.add(queryByBrand_cb);
        upside_panel.add(queryByBrand_btn);
        container.add(upside_panel,"North");

        //中
        CardLayout cardLayout = new CardLayout(20, 20);//设置布局管理器为卡片式
        middle_panel.setLayout(cardLayout);
        panel_1 = jPanelCar.getJPanelCar(query.adminQueryCar());
        middle_panel.add("查看汽车", panel_1);
        container.add(middle_panel, "Center");

        //下侧
        downside_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20));
        downside_panel.add(rent_btn);
        downside_panel.add(return_btn);
        downside_panel.add(record_btn);
        downside_panel.add(backup_btn);
        container.add(downside_panel, "South");


        queryCar_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cardLayout.show(middle_panel, "查看汽车");
            }
        });

        sortByRentASC_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel_2 = jPanelCar.getJPanelCar(query.adminSortCarByRentASC());
                middle_panel.add("按价格升序", panel_2);
                cardLayout.show(middle_panel, "按价格升序");
            }
        });

        sortByRentDESC_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel_3  = jPanelCar.getJPanelCar(query.adminSortCarByRentDESC());
                middle_panel.add("按价格降序", panel_3);
                cardLayout.show(middle_panel, "按价格降序");
            }
        });

        queryByCategory_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String category = String.valueOf(queryByCategory_cb.getSelectedItem());
                panel_4 = jPanelCar.getJPanelCar (query.adminQueryCarByCategory(category));
                middle_panel.add("按汽车类型搜索", panel_4);
                cardLayout.show(middle_panel, "按汽车类型搜索");
            }
        });

        queryByBrand_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String brand = String.valueOf(queryByBrand_cb.getSelectedItem());
                panel_5 = jPanelCar.getJPanelCar(query.adminQueryCarByBrand(brand));
                middle_panel.add("按汽车品牌搜索", panel_5);
                cardLayout.show(middle_panel, "按汽车品牌搜索");
            }
        });

        record_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                List<Record_User_Car> list = query.adminQueryRecord();
                panel_6 = jPanelRecord.getJPanelRecord(list);
                middle_panel.add("全部租车记录", panel_6);
                cardLayout.show(middle_panel, "全部租车记录");
            }
        });

        rent_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new AddUI(user);
            }
        });

        return_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new AlterUI(user);
            }
        });
        backup_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new LoginUI();
            }
        });

    }

    public void hide(){
        frame.setVisible(false);
    }
}
